package org.infodavid.common.services;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IUserGroupListItem;
import org.infodavid.common.impl.model.criteria.UserGroupCriteriaImpl;
import org.infodavid.common.model.AbstractBeanTest;
import org.infodavid.common.model.GroupTest;
import org.infodavid.common.model.IUserGroup;
import org.infodavid.common.model.criteria.IUserGroupCriteria;
import org.infodavid.common.persistence.UserGroupDataService;
import org.infodavid.common.services.UserGroupService;

/**
 * The Class GroupServiceTest.
 */
@SuppressWarnings({
  "null"
})
public class GroupServiceTest extends
    AbstractServiceTest<IUserGroupListItem,IUserGroup,IUserGroupCriteria,UserGroupDataService> {

  /** The delegate. */
  private final GroupTest delegate = new GroupTest(this);

	/**
   * The Constructor.
   */
  public GroupServiceTest() {
    super(UserGroupService.getInstance());
  }

  /**
   * See super class or interface. (non-Javadoc)
   * @return the data service class
   * @see org.infodavid.common.services.AbstractServiceTest#getDataServiceClass()
   */
  @Override
  public Class<UserGroupDataService> getDataServiceClass() {
    return UserGroupDataService.class;
  }

  /**
   * See super class or interface. (non-Javadoc)
   * @see org.infodavid.common.services.AbstractServiceTest#buildCriteria()
   */
  @Override
  public IUserGroupCriteria buildCriteria() {
    final IUserGroupCriteria criteria = new UserGroupCriteriaImpl();

    criteria.setNamePattern("name");

    return criteria;
  }

  /**
   * Test find by name.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByName() throws Exception {
    final IUserGroup value =populate(ITEMS_TO_INSERT).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByName(getContextMock(), value.getName());
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNull("An exception occurs", exception);
    Assert.assertNotNull("Data is null", value);
    Assert.assertNotNull("Key is null", value.getKey());
  }

  /**
   * Test testFindByName.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByNameWithoutValue() throws Exception {
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByName(getContextMock(), null);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNotNull("An exception occurs", exception);
    Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
  }

  /**
   * Test testFindByName.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByNameUsingAnInvalidContext() throws Exception {
    final IUserGroup value = populate(ITEMS_TO_INSERT).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByName(null, value.getName());
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNotNull("An exception occurs", exception);
    Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
  }

  /**
   * Test add.
   * @throws Exception the exception
   */
  @Test
  public final void testAddWithoutName() throws Exception {
    final IUserGroup value =create(1).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    value.setName(null);

    try {
      getService().add(getContextMock(), value);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNotNull("An exception occurs", exception);
    Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
  }

  /**
   * Test find by name.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByRole() throws Exception {
    final List<IUserGroup> results = new ArrayList<IUserGroup>();
    final IUserGroup value = populate(ITEMS_TO_INSERT).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByRole(getContextMock(), value.getRole(), results, null);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNull("An exception occurs", exception);
    Assert.assertNotNull("Data is null", value);
    Assert.assertNotNull("Key is null", value.getKey());
  }

  /**
   * Test testFindByName.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByRoleUsingAnInvalidContext() throws Exception {
    final List<IUserGroup> results = new ArrayList<IUserGroup>();
    final IUserGroup value = populate(ITEMS_TO_INSERT).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByRole(null, value.getRole(), results, null);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNotNull("An exception occurs", exception);
    Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
  }

  /**
   * Test testFindByName.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByRoleWithoutValue() throws Exception {
    final List<IUserGroup> results = new ArrayList<IUserGroup>();
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByRole(getContextMock(), null, results, null);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNotNull("An exception occurs", exception);
    Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
  }

  /**
   * Test find by name.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByGuest() throws Exception {
    final List<IUserGroup> results = new ArrayList<IUserGroup>();
    final IUserGroup value = populate(ITEMS_TO_INSERT).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByGuest(getContextMock(), value.isGuest(), results, null);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNull("An exception occurs", exception);
    Assert.assertNotNull("Data is null", value);
    Assert.assertNotNull("Key is null", value.getKey());
  }

  /**
   * Test testFindByName.
   * @throws Exception the exception
   */
  @Test
  public final void testFindByGuestUsingAnInvalidContext() throws Exception {
    final List<IUserGroup> results = new ArrayList<IUserGroup>();
    final IUserGroup value = populate(ITEMS_TO_INSERT).get(0);
    Exception exception = null;

    expectationsOnPersistenceSession();
    expectationsOnSecureService();

    try {
      ((UserGroupService)getService()).findByGuest(null, value.isGuest(), results, null);
    }
    catch (final Exception e) {
      e.printStackTrace();
      exception = e;
    }

    Assert.assertNotNull("An exception occurs", exception);
    Assert.assertEquals("Wrong exception", IllegalArgumentException.class, exception.getClass());
  }

	/*
   * (non-Javadoc)
   * @see org.infodavid.common.persistence.AbstractDataServiceTest#getBeanTest()
   */
  @Override
  protected AbstractBeanTest<IUserGroup> getBeanTest() {
  	return delegate;
  }
}
